package com.cheyunkeji.er.activity.fast_evaluate;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.Constants;
import com.cheyun.netsalev3.MyApplication;
import com.cheyunkeji.er.base.BaseActivity;
import com.cheyunkeji.er.utils.version.UpdatePresenter;
import com.cheyunkeji.er.view.SToast;
import com.cheyunkeji.er.view.TopBar;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.ButterKnife;

public class CallCenterActivity extends BaseActivity {

    private final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 6;
    @BindView(R.id.call_topbar)
    TopBar callTopbar;
    @BindView(R.id.rl_call)
    RelativeLayout rlCall;
    @BindView(R.id.version_number)
    TextView versionNumber;
    @BindView(R.id.updateLayout)
    RelativeLayout updateLayout;

    @Override
    protected void init() {
        setContentView(R.layout.er_activity_call_center);
        ButterKnife.bind(this);
    }

    @Override
    protected void initView() {
        callTopbar.setTitle("客服中心");
        callTopbar.setLeftBack();

        if (MyApplication.getInstance().getCurrentModuleType() == Constants.CURRENT_MODULE_TYPE_FAST_EVALUATE) {
            callTopbar.setBackgroundColor(getResources().getColor(R.color.bkg_blue));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                getWindow().setStatusBarColor(getResources().getColor(R.color.bkg_blue));
        } else if (MyApplication.getInstance().getCurrentModuleType() == Constants.CURRENT_MODULE_TYPE_FULL_EVALUATE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                getWindow().setStatusBarColor(getResources().getColor(R.color.auction_status_bar));
            callTopbar.setBackgroundColor(getResources().getColor(R.color.auction_status_bar));
        } else if (MyApplication.getInstance().getCurrentModuleType() == Constants.CURRENT_MODULE_TYPE_AUCTION) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                getWindow().setStatusBarColor(getResources().getColor(R.color.auction_status_bar));
            callTopbar.setBackgroundColor(getResources().getColor(R.color.auction_status_bar));
        }

        rlCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(CallCenterActivity.this,
                        Manifest.permission.CALL_PHONE)
                        != PackageManager.PERMISSION_GRANTED) {

                    ActivityCompat.requestPermissions(CallCenterActivity.this,
                            new String[]{Manifest.permission.CALL_PHONE},
                            MY_PERMISSIONS_REQUEST_CALL_PHONE);
                } else {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    Uri data = Uri.parse("tel:" + "057187030075");
                    intent.setData(data);
                    startActivity(intent);
                }
            }
        });

        try {
            versionNumber.setText("版本:" + getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_CONFIGURATIONS).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        updateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                UpdatePresenter presenter = new UpdatePresenter(CallCenterActivity.this, 3);
                presenter.setToas(true);
                presenter.getVersionInfo();

            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_CALL_PHONE:

                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Intent intent = new Intent(Intent.ACTION_CALL);
                    Uri data = Uri.parse("tel:" + "057187030075");
                    intent.setData(data);
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    startActivity(intent);
                } else {
                    SToast.show("无法拨打电话");
                }

                break;

            default:
                break;
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void initData() {

    }
}
